//
//  EPMHomeDAL.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/4/13.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMHomeDAL.h"

@implementation EPMHomeDAL

#pragma
#pragma-删除过期数据
+ (void)deleteCache{
    
    //一周前数据
    NSDate* currentDate = [NSDate dateWithTimeIntervalSinceNow:-7*24*60*60];
    //时间格式化
    NSDateFormatter* formatter = [NSDateFormatter new];
    formatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    NSString* dateStr = [formatter stringFromDate:currentDate];
    NSString* sql = [NSString stringWithFormat:@"DELETE FROM T_Home WHERE createtime<%@",dateStr];
    
    [[EPMSQLTool sheardTool].queue inDatabase:^(FMDatabase *db) {
        //执行sql语句
        if ( [db executeUpdate:sql withArgumentsInArray:0]){
            
        }else{
            
        }
    }];
    
}
#pragma
#pragma-检查缓存数据

+ (void)checkCacheSinceID:(int64_t) sinceID maxID:(int64_t)maxID finished:(void(^)(NSArray* infoArr))finishedBlock{
    NSArray* res = [self getCacheSinceID:sinceID maxID:maxID];
    if (res.count > 0){
        if (finishedBlock){
            finishedBlock(res);
        }
    }else{
        //从网络加载数据
       [ [EPMNetWorkTool sheardTool] getHomeWeiBoDataMaxID:maxID sinceID:sinceID finished:^(id res, NSError *error) {
            if(error != nil){
                finishedBlock(nil);
                return ;
            }
           NSArray* tempArray = res;
           if (tempArray.count == 0){
               finishedBlock(nil);
               return ;
           }
           [self saveCache:tempArray];
           finishedBlock(tempArray);
           
       }];
    }
    
}
#pragma
#pragma-获取缓存
+ (NSArray*) getCacheSinceID:(int64_t)sinceID maxID:(int64_t)maxID {
    
    NSMutableArray* arrayM = [NSMutableArray new];
    //初次登录加载网络数据
    if ( sinceID == 0 && maxID == 0){
        return arrayM;
    }
    
    //准备SQL
    NSMutableString* sql = [NSMutableString new];
    [sql appendString:@"SELECT * FROM T_Home\n"];
    [sql appendString:[NSString stringWithFormat:@"WHERE userid=%@\n",[EPMUserAccountModelView sheardTool].account.uid]];
    if (sinceID > 0){
        [sql appendString:[NSString stringWithFormat:@"AND statusid>%lld\n",sinceID]];
    }
    if ( maxID > 0){
        [sql appendString:[NSString stringWithFormat:@"AND statusid<=%lld\n",maxID]];
    }
    [sql appendString:@"ORDER BY statusid DESC\n"];
    [sql appendString:@"LIMIT 20"];
    
    NSArray* temp = [[EPMSQLTool sheardTool] selectDataWithSQL:sql];
    //字典转模型
    for ( NSDictionary* dict in temp){
        NSData* data = dict[@"status"];
        NSDictionary* tempDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        [arrayM addObject:tempDict];
    }
    return arrayM;
    
}

#pragma
#pragma-存储数据
+ (void)saveCache:(NSArray*) temp{
    
    NSString* userID = [EPMUserAccountModelView sheardTool].account.uid;
    //准备SQL
    NSString* sql = @"INSERT OR REPLACE INTO T_Home (status,statusid,userid) VALUES (?,?,?)";
    //带回滚写入
    [[EPMSQLTool sheardTool].queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for (NSDictionary* dict in temp){
            int64_t statusid = [dict[@"id"] longLongValue];
            NSData* data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
            if ([db executeUpdate:sql withArgumentsInArray:@[data,@(statusid),userID]]){
                
            }else{
                [db rollback];
            }
            
        }
        
        
    }];
    
}


@end
